﻿//===============================================================================
//	LuaGlueMission.cpp
//	LUA -> C++ 地图相关接口管理器
//===============================================================================



#include "LuaGlue.h"
#include "../share/LuaGlueCommon.h"

#include "Mission.h"
#include "MissionDialogue.h"

#include "LuaManager.h"
#include "../share/user_define.h"

#include "ManagerHelper.h"
#include "Player.h"

#include "script_define.h"

#include <vector>
#include "scene_logic.h"
#include "MapSlot.h"

#include "../share/MessageDef.h"
//-------------------------------------------------------------------------------
int32_t Mis_GetEmptyMissionPos(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "Mis_GetEmptyMissionPos");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_GetEmptyMissionPos");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			uint32_t _idx = _ms->GetAvailablePos();
			MacroPushInteger(ls, _idx);
			return 1;
		}
	}

	MacroPushInteger(ls, -1);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}


//	获取当前主线任务ID
int32_t Mis_GetMainMissionId(lua_State *ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "Mis_GetMainMissionId");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_GetMainMissionId");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			uint32_t misnId = _ms->GetMainMissionId();
			MacroPushInteger(ls, misnId);
			return 1;
		}
	}

	MacroPushInteger(ls, -1);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t Mis_GetIndexByMissionID(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "Mis_GetIndexByMissionID");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_GetIndexByMissionID");
	MacroIsNumber(ls, ++_pos, "Mis_GetIndexByMissionID");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	uint32_t _mid = MacroGetUnsignedIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	int32_t _idx = _ms->GetIndexByMissionID(_mid);
	
	MacroPushInteger(ls, _idx);
	return 1;
	
	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;

}

int32_t Mis_GetMissionIdByIndex(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "Mis_GetMissionIdByIndex");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_GetMissionIdByIndex");
	MacroIsNumber(ls, ++_pos, "Mis_GetMissionIdByIndex");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	uint32_t _idx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	int32_t _misnId = _ms->GetMissionId(_idx);
	MacroPushInteger(ls, _misnId);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t Mis_GetMissionLimitLevelByIndex(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "Mis_GetIndexByMissionID");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_GetIndexByMissionID");

	_pos = 0;
	uint32_t _mid = MacroGetIntegerArgument(ls, ++_pos);

	TaskData* _pData = TaskDataTablePtr->GetTaskDataById(_mid);

	uint32_t limitLv = _pData->m_level;

	MacroPushInteger(ls, limitLv);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;

}

//-------------------------------------------------------------------------------
int32_t Mis_SetMissionToUser(lua_State * ls)
{
	_safe_guard_begin;
	
	printf("%s\n", "Mis_SetMissionToUser start.");

	MacroCheckParamCount(ls, 6, "Mis_SetMissionToUser");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionToUser");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionToUser");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionToUser");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionToUser");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionToUser");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionToUser");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);
	uint32_t _mid = MacroGetUnsignedIntegerArgument(ls, ++_pos);
	uint32_t _sid = MacroGetUnsignedIntegerArgument(ls, ++_pos);
	//uint32_t _npcid = MacroGetUnsignedIntegerArgument(ls, ++_pos);
	uint32_t _npcid = (uint32_t)MacroGetNumberArgument(ls, ++_pos);//!42亿截断(21)，小手勿动！
	uint32_t _type = MacroGetUnsignedIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			//_ms[_idx].Clear();
			_ms->SetScirptId(_idx, _sid);
			_ms->SetMissionId(_idx, _mid);
			_ms->SetNpcId(_idx, _npcid);
			_ms->SetUsed(_idx);
			_ms->SetMissionType(_idx, _type);

			//+ 日常任务的redis更新
			if (2 == _type)
			{
				//_player->SendDailyMissionList();//解锁新日常任务推送

				//_player->SetDailyMissionFlagByFlag(UpdateFixedSDTType::day_flage);
			}

			return 0;
		}
	}

	//MacroPushInteger(ls, -1);
	//printf("%s\n", "Mis_SetMissionToUser end.");
	//return 1;
	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;

}

//-------------------------------------------------------------------------------
int32_t Mis_MissionFinished(lua_State * ls)
{
	_safe_guard_begin;
	MacroCheckParamCount(ls, 2, "Mis_MissionFinished");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_MissionFinished");
	MacroIsNumber(ls, ++_pos, "Mis_MissionFinished");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			_ms->SetAvailable(_idx);

			return 0;
		}

	}

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;

}

//-------------------------------------------------------------------------------
int32_t Mis_SetMissionFlag(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 4, "Mis_SetMissionFlag");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_SetMissionFlag");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _flag_idx = MacroGetIntegerArgument(ls, ++_pos);
	uint32_t _flag_val = (uint32_t)MacroGetNumberArgument(ls, ++_pos);//!42亿截断(21)，小手勿动！

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			_ms->SetMissionInfo(_idx, _flag_idx, _flag_val);

			return 0;
		}
	}

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;

}

//-------------------------------------------------------------------------------
int32_t Mis_GetMissionFlag(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 3, "Mis_GetMissionFlag");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_GetMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_GetMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_GetMissionFlag");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _flag_idx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			int32_t _flag_val = _ms->GetMissionInfo(_idx, _flag_idx);

			MacroPushInteger(ls, _flag_val);
			return 1;
		}
	}

	MacroPushInteger(ls, -1);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;

}

int32_t Mis_SetDailyMissionFlag(lua_State * ls)
{
	_safe_guard_begin;

	printf("%s\n", "Mis_SetDailyMissionFlag start.");

	MacroCheckParamCount(ls, 4, "Mis_SetDailyMissionFlag");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_SetDailyMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_SetDailyMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_SetDailyMissionFlag");
	MacroIsNumber(ls, ++_pos, "Mis_SetDailyMissionFlag");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _flag_idx = MacroGetIntegerArgument(ls, ++_pos);
	uint32_t _flag_val = (uint32_t)MacroGetNumberArgument(ls, ++_pos);//!42亿截断(21)，小手勿动！

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (_player != nullptr)
	{
		MissionSystem* _ms = _player->GetMissionSystem();
		if (_ms != nullptr)
		{
			_ms->SetMissionInfo(_idx, _flag_idx, _flag_val);

			//////////////////////////////////////////////////////////////////////////////
			//_player->SetDailyMissionFlagByFlag(UpdateFixedSDTType::day_flage);

			return 0;
		}
	}

	//MacroPushInteger(ls, -1);
	//printf("%s\n", "Mis_SetDailyMissionFlag end.");

	//return 1;
	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

//-------------------------------------------------------------------------------
int32_t MisDlg_SendMissionList(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MisDlg_SendMissionList");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MisDlg_SendMissionList");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->GetMissionList();

	return 0;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
int32_t MisDlg_SendMissionCompleteList(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MisDlg_SendMissionCompleteList");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MisDlg_SendMissionCompleteList");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->GetMissionCompleteState();

	return 0;

	_safe_guard_end;

	return 0;
}

//-------------------------------------------------------------------------------
int32_t Mis_ResetMissionStateToLastByLuaIndex(lua_State *ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "Mis_ResetMissionStateElseByLuaIndex");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mis_ResetMissionStateElseByLuaIndex");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	//_player-> 需新增处理接口

	return 0;

	_safe_guard_end;

	return 0;
}


int32_t Mission_GetInfoArrayData(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "Mission_GetInfoArrayData");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "Mission_GetInfoArrayData");
	MacroIsNumber(ls, ++_pos, "Mission_GetInfoArrayData");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnIdx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _pMissionSys = _player->GetMissionSystem();
	_safe_check(_pMissionSys);

	auto _pMissionNode = _pMissionSys->GetMissionInfoByIndex(_misnIdx);
	_safe_check(_pMissionNode);

	uint32_t _count = _pMissionNode->m_info.GetMaxCount();

	for (uint32_t i = 0; i < _count; ++i)
	{
		MacroPushInteger(ls, _pMissionNode->m_info.GetValue(i)); 
	}
	return _count;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t DailyMission_ListClear(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_ListClear");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_ListClear");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _pMissionSys = _player->GetMissionSystem();
	_safe_check(_pMissionSys);

	_pMissionSys->ClearDailyMissionlist();

	MacroPushInteger(ls, 1);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;

}

int32_t DailyMission_ConfigCount(lua_State * ls)
{
	_safe_guard_begin;

	uint32_t _count = DailyMissionCnfgTablePtr->GetDailyMissionCount();
	if (0 != _count)
	{
		MacroPushInteger(ls, _count);
		return 1;
	}

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t DailyMission_ConfigInfo(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_ConfigInfo");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_ConfigInfo");

	_pos = 0;
	int32_t _snPos = MacroGetIntegerArgument(ls, ++_pos);

	DailyMissionData* pmisnDaily = DailyMissionCnfgTablePtr->GetMissionDailyDataByIdx(_snPos);
	_safe_check(pmisnDaily);

	MacroPushInteger(ls, pmisnDaily->m_missionId);
	MacroPushInteger(ls, pmisnDaily->m_scriptId);
	return 2;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t DailyMission_GetStartAndEndPos(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_GetStartAndEndPos");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetStartAndEndPos");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	auto _pMisnSys = _player->GetMissionSystem();
	_safe_check(_pMisnSys);

	uint32_t _sPos = _pMisnSys->GetDailyMissionPosStart();
	uint32_t _ePos = _pMisnSys->GetDailyMissionPosEnd();

	MacroPushInteger(ls, _sPos);
	MacroPushInteger(ls, _ePos);
	return 2;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t DailyMission_InThelist(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "DailyMission_InThelist");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_InThelist");
	MacroIsNumber(ls, ++_pos, "DailyMission_InThelist");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnId = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	uint32_t _ret = _player->CheckUnlockedMissionInDailyList(_misnId);

	MacroPushInteger(ls, _ret);
	return 1;

	_safe_guard_end;


	return 0;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_GetReleasedState(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "DailyMission_GetReleasedState");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetReleasedState");
	MacroIsNumber(ls, ++_pos, "DailyMission_GetReleasedState");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnId = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	//	日常任务配置表
	DailyMissionData* pMisnData = DailyMissionCnfgTablePtr->GetDailyMissionDataById(_misnId);
	_safe_check(pMisnData);

	MacroPushInteger(ls, pMisnData->m_released);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_GetRequirLimit(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_GetRequirLimit");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetRequirLimit");

	_pos = 0;
	int32_t _dailyMissionId = MacroGetIntegerArgument(ls, ++_pos);

	DailyMissionData* pDaily = DailyMissionCnfgTablePtr->GetDailyMissionDataById(_dailyMissionId);
	_safe_check(pDaily);

	uint32_t requirLimit = pDaily->m_missionTimes;

	MacroPushInteger(ls, requirLimit);
	return 1;

	_safe_guard_end;
	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_GetActiveLimit(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_GetActiveLimit");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetActiveLimit");

	_pos = 0;
	int32_t _dailyMissionId = MacroGetIntegerArgument(ls, ++_pos);

	DailyMissionData* pDaily = DailyMissionCnfgTablePtr->GetDailyMissionDataById(_dailyMissionId);
	_safe_check(pDaily);

	uint32_t activeLimit = pDaily->m_activeValue;

	MacroPushInteger(ls, activeLimit);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_GetEmptyPosByErea(lua_State * ls)
{
	_safe_guard_begin;
	MacroCheckParamCount(ls, 1, "DailyMission_GetEmptyPosByErea");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetEmptyPosByErea");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	uint32_t _idx = _ms->GetDailyAvailablePosByErea();

	MacroPushInteger(ls, _idx);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_GetUnlockSystemId(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_GetUnlockSystemId");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetUnlockSystemId");

	_pos = 0;
	int32_t _misnId = MacroGetIntegerArgument(ls, ++_pos);

	DailyMissionData* pDaily = DailyMissionCnfgTablePtr->GetDailyMissionDataById(_misnId);
	_safe_check(pDaily);

	if (nullptr != pDaily)
	{
		uint32_t functionId = pDaily->m_unlockSysId;

		MacroPushInteger(ls, functionId);
		return 1;
	}

	MacroPushInteger(ls, -1);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_GetUnlockSystemData(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_GetUnlockSystemData");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_GetUnlockSystemData");

	_pos = 0;
	int32_t _functionId = MacroGetIntegerArgument(ls, ++_pos);

	FunctionUnlockData* pFunction = FunctionUnlockCnfgTablePtr->GetFunctionUnlockDataById(_functionId);
	_safe_check(pFunction);

	int32_t unlockSysType = pFunction->m_unlockSysType;
	int32_t unlockSysVal = pFunction->m_condition_parameter;

	MacroPushInteger(ls, unlockSysType);
	MacroPushInteger(ls, unlockSysVal);
	return 2;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t DailyMission_CheckUnlockData(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_CheckUnlockData");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_CheckUnlockData");

	_pos = 0;
	int32_t _functionId = MacroGetIntegerArgument(ls, ++_pos);

	auto* _pDta = MapSlot::GetInstance().GetAffiexNodeDataByKey(_functionId);
	if ( nullptr == _pDta)
	{
		return 0;
	}
	
	int32_t misnId = _pDta->m_missionId;
	int32_t scriptId = _pDta->m_scriptId;

	MacroPushInteger(ls, misnId);
	MacroPushInteger(ls, scriptId);
	return 2;

	_safe_guard_end;

	return 0;
}


//-------------------------------------------------------------------------------
int32_t DailyMission_GetPlayerValueAboutUnlockSys(lua_State * ls)
{
	_safe_guard_begin;

	return -1;

	_safe_guard_end;

	return -1;
}

//-------------------------------------------------------------------------------
int32_t DailyMission_RewardBoxInfo(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_RewardBoxInfo");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_RewardBoxInfo");

	_pos = 0;
	int32_t _pidx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pidx);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->GetMissionBoxInfoToClient();

	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t DailyMission_SendList(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "DailyMission_SendList");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "DailyMission_SendList");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->SendDailyMissionList();

	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t MissionOfferReward_GetStartPos(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_GetStartPos");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetStartPos");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	uint32_t _indexPos = _ms->GetOfferRewardMissionStartPos();

	MacroPushInteger(ls, _indexPos);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t MissionOfferReward_SetStar(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 3, "MissionOfferReward_SetStar");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_SetStar");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_SetStar");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_SetStar");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnIdx = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _star = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	_ms->SetOfferRewardMissionStars(_misnIdx, _star);

	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t MissionOfferReward_ListToClient(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_ListToClient");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_ListToClient");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->GetOfferRewardMissionListToClient();
	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t MissionOfferReward_ClearByIdx(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "MissionOfferReward_ClearByIdx");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_ClearByIdx");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_ClearByIdx");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnIdx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	_ms->ClearOfferRewardMissionByIdx(_misnIdx);
	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t MissionOfferReward_GetAvailablePos(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_GetAvailablePos");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetAvailablePos");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	int32_t pos = _ms->GetOfferRewardMissionAvailablePosByErea();
	
	MacroPushInteger(ls, pos);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t MissionOfferReward_GetIndexById(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "MissionOfferReward_GetIndexById");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetIndexById");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetIndexById");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnId = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	int32_t index = _ms->GetOfferRewardMissionIdxById(_misnId);

	MacroPushInteger(ls, index);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t MissionOfferReward_ResetByIndex(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "MissionOfferReward_ResetByIndex");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_ResetByIndex");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_ResetByIndex");

	_pos = 0;
	int32_t _pid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _misnIdx = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_pid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	_ms->ResetOfferRewardMissionByIndex(_misnIdx);

	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t MissionOfferReward_GetUpdateCostByIdxZero(lua_State * ls)
{
	_safe_guard_begin;

	//根据【购买重置表】中格式，按读取顺序确定interface名字--IdxZero
	uint32_t index = 0;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_GetUpdateCostByIdxZero");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetUpdateCostByIdxZero");

	_pos = 0;
	int32_t _times = MacroGetIntegerArgument(ls, ++_pos);

	BuyResetData* _data = BuyResetTablePtr->GetBuyResetDataByIdx(index);
	_safe_check(_data);

	//uint32_t costVal = _data->mBuyReward[_times];!越界
	uint32_t costVal = _data->GetCostValueByIndex(_times);

	MacroPushInteger(ls, costVal);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t MissionOfferReward_CompleteImmediatelyCost(lua_State * ls)
{
	_safe_guard_begin;

	//根据【购买重置表】中格式，按读取顺序确定interface名字--IdxZero
	uint32_t index = 0;
	BuyResetData* _data = BuyResetTablePtr->GetBuyResetDataByIdx(index);
	_safe_check(_data);

	uint32_t costVal = _data->mFinishReward;

	MacroPushInteger(ls, costVal);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t MissionOfferReward_GetStarLevel(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "MissionOfferReward_GetStarLevel");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetStarLevel");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetStarLevel");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);

	

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	MissionSystem* _ms = _player->GetMissionSystem();
	_safe_check(_ms);

	mission* _offer_ms = _ms->GetMissionInfoByIndex(_idx);
	_safe_check(_offer_ms);

	uint32_t starLv = _offer_ms->m_star;

	MacroPushInteger(ls, starLv);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t Mission_UpdateItemInfo(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "MissionOfferReward_GetStarLevel");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetStarLevel");
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetStarLevel");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _idx = MacroGetIntegerArgument(ls, ++_pos);


	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->UpdateMissionItemInfo(_idx);

	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}

int32_t MissionOfferReward_GetRequirValue(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_GetRequirValue");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetRequirValue");
	
	_pos = 0;
	int32_t _mid = MacroGetIntegerArgument(ls, ++_pos);


	RewardMissionData* _pData = RewardMissionCnfgTablePtr->GetRewardMissionDataById(_mid);
	if (nullptr == _pData)
	{
		return 0;
	}

	_safe_check(_pData);


	uint32_t _requirVal = _pData->m_requir;

	MacroPushInteger(ls, _requirVal);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t MissionOfferReward_GetTargetId(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_GetTargetId");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetTargetId");

	_pos = 0;
	int32_t _mid = MacroGetIntegerArgument(ls, ++_pos);


	RewardMissionData* _pData = RewardMissionCnfgTablePtr->GetRewardMissionDataById(_mid);
	if (nullptr == _pData)
	{
		return 0;
	}

	_safe_check(_pData);

	uint32_t _targetId = _pData->m_taskTarget;

	MacroPushNumber(ls, _targetId);
	return 1;

	_safe_guard_end;

	MacroPushNumber(ls, -1);
	return 1;
}

int32_t MissionOfferReward_GetStartAndEndPos(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_GetStartAndEndPos");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_GetStartAndEndPos");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	auto _pMisnSys = _player->GetMissionSystem();
	_safe_check(_pMisnSys);

	uint32_t _sPos = _pMisnSys->GetOfferRewardMissionStartPos();
	uint32_t _ePos = _pMisnSys->GetOfferRewardMissionEndPos();

	MacroPushInteger(ls, _sPos);
	MacroPushInteger(ls, _ePos);
	return 2;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t MissionOfferReward_CheckMissionListIsExist(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 1, "MissionOfferReward_CheckMissionListIsExist");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MissionOfferReward_CheckMissionListIsExist");

	_pos = 0;
	int32_t _uid = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_uid);
	if (nullptr == _player)
	{
		MacroPushInteger(ls, -1);
		return 1;
	}
	_safe_check(_player);

	auto _pMisnSys = _player->GetMissionSystem();
	_safe_check(_pMisnSys);

	uint32_t _sPos = _pMisnSys->GetOfferRewardMissionStartPos();
	uint32_t _ePos = _pMisnSys->GetOfferRewardMissionEndPos();

	MacroPushInteger(ls, 1);
	return 1;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

//-------------------------------------------------------------------------------
int32_t Title_BalanceByTime(lua_State * ls)
{
	_safe_guard_begin;

	return 0;

	_safe_guard_end;

	MacroPushInteger(ls, -1);
	return 1;
}

int32_t MainMission_DungeonClearance(lua_State * ls)
{
	_safe_guard_begin;

	MacroCheckParamCount(ls, 2, "MainMission_DungeonClearance");

	uint32_t _pos = 0;
	MacroIsNumber(ls, ++_pos, "MainMission_DungeonClearance");
	MacroIsNumber(ls, ++_pos, "MainMission_DungeonClearance");

	_pos = 0;
	int32_t _account = MacroGetIntegerArgument(ls, ++_pos);
	int32_t _dungeonId = MacroGetIntegerArgument(ls, ++_pos);

	Player* _player = CSceneLogic::GetInstance().GetPlayerByPlayerID(_account);
	if (nullptr == _player)
	{
		return 0;
	}
	_safe_check(_player);

	_player->DungeonMission(_dungeonId);

	return 0;

	_safe_guard_end;

	//MacroPushInteger(ls, -1);
	//return 1;
	return 0;
}